package com.taotao.service.impl;

import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.service.ItemParamItemService;
import com.taotao.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by new on 2018/8/11.
 */
@Service
public class ItemParamItemServiceImpl implements ItemParamItemService{

    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    @Override
    public String getItemParamByItemId(Long itemId) {
        TbItemParamItemExample example = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> list = tbItemParamItemMapper.selectByExampleWithBLOBs(example);
        if(list == null || list.size() == 0){
            return "";
        }
        TbItemParamItem item = list.get(0);
        //取出规格参数
        String paramData = item.getParamData();
        //把规则参数json数据转换成对象
        List<Map> maps = JsonUtils.jsonToList(paramData, Map.class);
        //生成html
        StringBuffer sb = new StringBuffer();
        sb.append("<table cellpadding=\"0\" cellspacing=\"1\" border=\"1\" width=\"100%\">\n");
        sb.append("    <tbody>\n");
        for(Map m1 : maps){
            sb.append("    <tr>\n");
            sb.append("        <th colspan=\"2\">"+m1.get("group")+"</th>\n");
            sb.append("    </tr>\n");
            List<Map> maps1 = (List<Map>) m1.get("params");
            for(Map m2 : maps1){
                sb.append("    <tr>\n");
                sb.append("        <td>"+m2.get("k")+"</td>\n");
                sb.append("        <td>"+m2.get("v")+"</td>\n");
                sb.append("    </tr>\n");
            }
        }
        sb.append("    </tbody>\n");
        sb.append("</table>");

        return sb.toString();
    }
}
